package com.publicize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.publicize.commom.commonutils.Result;
import com.publicize.commom.servicebase.exception.MyException;
import com.publicize.entity.Manage;
import com.publicize.entity.Module;
import com.publicize.entity.Vo.QueryVo;
import com.publicize.entity.Vo.ResultVo;
import com.publicize.mapper.ModuleMapper;
import com.publicize.service.ModuleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.publicize.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 投稿信息表 服务实现类
 * </p>
 *
 * @author bs
 * @since 2021-03-03
 */
@Service
public class ModuleServiceImpl extends ServiceImpl<ModuleMapper, Module> implements ModuleService {
    @Resource
    private UserService userService;
    @Resource
    private ModuleMapper moduleMapper;
    //根据模块id查询对应未审核分页稿子
    @Override
    public Result selectModuleById(String relationshipId, Integer page, Integer limit) {
        //根据模块id查询对应稿子
        QueryWrapper<Module> queryWrapper=new QueryWrapper<>();
        //根据分页数查询
        Page<Module> pages=new Page<>(page,limit);
        //根据模块id查询
        queryWrapper.eq("relationship_id",relationshipId);
        //查询未审核稿子
        queryWrapper.eq("is_check",0);
        IPage<Module> moduleIPage = baseMapper.selectPage(pages, queryWrapper);
        //返回总数目
        long total = moduleIPage.getTotal();
        //返回总数据
        List<Module> records = moduleIPage.getRecords();
        //返回分页页数
        long current = moduleIPage.getCurrent();
        return Result.ok().data("total",total).data("current",current).data("records",records);
    }
    //设置审核是否通过
    @Override
    public void statusManage(String id,Integer status) {
        QueryWrapper<Module> queryWrapper=new QueryWrapper<>();
        Module module=new Module();

        module.setStatus(status);
        module.setId(id);
        int i = baseMapper.updateById(module);
        if(i!=1) throw new MyException("审核失败",201);
    }
    //根据条件查询
    @Override
    public Map<String,Object> selectByConditions(QueryVo queryVo) {
        List<ResultVo> resultVos = moduleMapper.selectByConditions(queryVo);
        int moduleCount = baseMapper.getModuleCount(queryVo);
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("resultVos",resultVos);
        resultMap.put("moduleCount",moduleCount);
        return resultMap;
    }

}
